<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { Toastloading } from '@/utils/public';
  import xmImage from '@/components/xmImage/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import plateForm from './plateForm.vue';
  const plateFormRef = ref()
  const props = defineProps({
    serachList:{
      type:Boolean,
      default:false
    },
    nameplate_id:{
      type:Number,
      default:0
    }
  }),
  $emit = defineEmits(['change'])
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page:1,
    limit:10,
    frame_name:'',
    frame_id:'',
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
  listQuery.page = 1;
  getList();
};
const resetQuery = () => {
  Object.assign(listQuery, {
    ...initialQuery
  });
};
  const handleForm = (row:any = {})=>{
    plateFormRef.value.openDialog(row)
  }
  const selectChange = (row:any = {})=>{
    $emit('change',row)
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await guild_roomApi.roomFrameList(listQuery)
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">装饰框ID：</div>
          <el-input :validate-event="false" v-model="listQuery.frame_id" placeholder="请输入装饰框牌ID"/>
        </div>
        <div class="labelItem">
          <div class="label">装饰框名称：</div>
          <el-input :validate-event="false" v-model="listQuery.frame_name" placeholder="请输入装饰框名称"/>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button v-if="!serachList" v-rule="'room_frameAdd'" type="primary" @click="handleForm()">新增装饰框</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="装饰框ID" />
      <el-table-column min-width="180" prop="frame_name" label="装饰框名称" />
      <el-table-column width="160" prop="frame_img" label="装饰框图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.frame_img" :list="[scoped.row.frame_img]" />
        </template>
      </el-table-column>
      <el-table-column min-width="280" show-overflow-tooltip prop="frame_desc" label="装饰框描述" />
      <el-table-column width="180" prop="created_at" label="创建时间" />
      <el-table-column fixed="right" width="120"  label="操作" >
        <template #default="scoped">
          <el-button v-if="!serachList" type="primary" v-rule="'room_frameEdit'" @click="handleForm(scoped.row)">编辑</el-button>
          <template v-if="serachList">
            <el-button v-if="nameplate_id != scoped.row.id" type="primary" @click="selectChange(scoped.row)">选择装饰框</el-button>
            <el-button v-else type="danger" @click="selectChange({})">取消选中</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <plateForm ref="plateFormRef" @update-list="getList" />
  </div>
</template>